﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Travasto.Model;

namespace Travasto.Web.Areas.Admin.ViewModes
{
    public class TourViewModel
    {
        public TourViewModel()
        {
            this.Description = "Hello";
            this.Tags = new List<Tag>();
            this.Packages = new List<Package>();
        }

        public Tour Tour
        {
            get { return GtTour(); }
            set { SetTour(value); }
        }
        private void SetTour(Tour tour)
        {
            this.TourId = tour.TourId;
            this.Title = tour.Title;
            this.Description = tour.Description;
            this.Tags = tour.Tags ?? new List<Tag>();
            this.Packages = tour.Packages ?? new List<Package>();
        }
        private Tour GtTour()
        {
            var tour = new Tour();
            tour.TourId = this.TourId;
            tour.Title = this.Title;
            tour.Description = this.Description;
            tour.Tags = this.Tags;
            tour.Packages = this.Packages;

            return tour;
        }

        public long TourId { get; set; }
        public string Title { get; set; }

        [UIHint("tinymce_jquery_full"), AllowHtml]
        public string Description { get; set; }

        public ICollection<Tag> Tags { get; set; }
        public ICollection<Package> Packages { get; set; }

        [Display(Name="Tags")]
        public string TagString
        {
            get { 
                var tags = this.Tags.Select(t => t.Name).ToArray();
                if (tags.Length > 0)
                {
                    return string.Join(", ", tags);
                }
                else
                {
                    return "";
                }
            }
            set 
            {
                this.Tags = value == null ? new List<Tag>() : value.Split(',').Where(t => t.Trim() != "").Select(t => new Tag() { Name = t.Trim() }).ToList(); 
            }
        }
    }
}